//
//  ZFAlertManager.m
//  components
//
//  Created by linzaifei on 2021/7/12.
//

#import "ZFAlertManager.h"
#import <objc/runtime.h>
@interface ZFAlertManager()
@property(nonatomic,strong)UIAlertController *alertController;
@property(nonatomic,copy)ActionBlock block;

@end

@implementation ZFAlertManager
-(ZFAlertManager* (^)(ZFAlertAction *action))addCustomAction{
    __weak typeof(self) weakSelf = self;
    return ^(ZFAlertAction *action){
        [weakSelf addAction:action];
        return self;
    };
}
///添加一个defaultAction title
-(ZFAlertManager*(^)(NSString *title))addDefaultTitle{
    return [self addCustomAction:UIAlertActionStyleDefault];
}
///添加一个cancelAction title
-(ZFAlertManager*(^)(NSString *title))addCencalTitle{
    return [self addCustomAction:UIAlertActionStyleCancel];
}
///添加一个DestructiveAction title
-(ZFAlertManager*(^)(NSString *title))addDestructiveTitle{
    return [self addCustomAction:UIAlertActionStyleDestructive];
}
///添加一个textField
-(ZFAlertManager*(^)(TextFiedBlock block))addTextField{
    NSAssert(self.preferredStyle != UIAlertControllerStyleActionSheet, @"actionSheet 不能添加textFied");
    __weak typeof(self) weakSelf = self;
    return ^(TextFiedBlock block){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            block(textField);
        }];
        return strongSelf;
    };
}
///公共样式
-(ZFAlertManager*(^)(NSString *title))addCustomAction:(UIAlertActionStyle)style{
    __weak typeof(self) weakSelf = self;
    return ^(NSString *title){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        ZFAlertAction *zfaction = [ZFAlertAction actionWithTitle:title style:style block:^(ZFAlertAction * _Nonnull action) {
            if(weakSelf.block){
                weakSelf.block(action);
            }
        }];
        return strongSelf.addCustomAction(zfaction);
    };
}

///回调
-(ZFAlertManager*)onDidClickAction:(ActionBlock)block{
    self.block = block;
    return  self;
}
-(ZFAlertManager*)showAlertInController:(UIViewController *)control animated:(BOOL)animated{
    NSAssert(self, @"需要初始化项目initWithTitle: message: preferredStyle:");
    if (control == nil) {
        [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:self animated:YES completion:nil];
    }else{
        [control presentViewController:self animated:animated completion:^{
            
        }];
    }
    return self;
}
-(void)dealloc{
    NSLog(@"alert销毁了");
}
@end


@implementation ZFAlertAction
+ (instancetype _Nonnull )actionWithTitle:( NSString *_Nonnull)title style:(UIAlertActionStyle)style block:(ActionBlock _Nullable )block{
    return [ZFAlertAction actionWithTitle:title style:style handler:^(UIAlertAction * action) {
        block( (ZFAlertAction *)action);
    }];
}



//按钮标题的字体颜色
-(void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    [self setValue:textColor forKey:@"titleTextColor"];
//    unsigned int count = 0;
//    Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
//    for(int i =0;i < count;i ++){
//        Ivar ivar = ivars[i];
//        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
//        if ([ivarName isEqualToString:@"_titleTextColor"]) {
//            [self setValue:textColor forKey:@"titleTextColor"];
//        }
//    }
}


@end
